//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 6
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;
using ProxyHelpers.EWS;

namespace ResolveNames
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            ResolveNames(binding, "tina", true);
        }

        /// <summary>
        /// Resolve the given ambiguous name (Listing 6-20)
        /// </summary>
        /// <param name="binding">Binding to use for call</param>
        /// <param name="name">Ambiguous name to resolve</param>
        /// <param name="returnFullContactData">If true will return full contact 
        /// data</param>
        /// 
        public static void ResolveNames(
                               ExchangeServiceBinding binding,
                               string name,
                               bool returnFullContactData)
        {
            ResolveNamesType request = new ResolveNamesType();
            request.ReturnFullContactData = returnFullContactData;

            // Set the ambiguous name we want to resolve
            //
            request.UnresolvedEntry = name;

            // Make the call
            //
            ResolveNamesResponseType response = binding.ResolveNames(request);
            ResolveNamesResponseMessageType responseMessage =
                  response.ResponseMessages.Items[0] as
                              ResolveNamesResponseMessageType;

            // Remember that ErrorNameResolutionMultipleResults is not necessarily 
            // an error
            //
            if ((responseMessage.ResponseCode == ResponseCodeType.NoError) ||
                (responseMessage.ResponseCode ==
                    ResponseCodeType.ErrorNameResolutionMultipleResults))
            {
                // The response message holds an array of ResolutionType instance - 
                // one for each matched name
                //
                foreach (ResolutionType entry in
                           responseMessage.ResolutionSet.Resolution)
                {
                    // do something with each result
                }
            }
        }

    }
}
